package com.heima.article.listener;

import com.heima.article.service.ApHotArticlesService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.util.JsonUtils;
import com.heima.model.article.dtos.ArticleBehaviorStreamMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class BehaviorListener {
    @Autowired
    private ApHotArticlesService apHotArticlesService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_SCORE_CONSUMER_TOPIC)
    public void getMsg(ConsumerRecord<String, String> record) {
        if (record == null) {
            return;
        }

        ArticleBehaviorStreamMsg articleBehaviorStreamMsg = JsonUtils.toBean(record.value(), ArticleBehaviorStreamMsg.class);
        log.info("接收到行为消息，{}", record.value());
        apHotArticlesService.updateHotArticle(articleBehaviorStreamMsg);

    }
}
